home *** CD-ROM | disk | FTP | other *** search
/ Inside Mac Games Volume 5 #3 / IMG 46 Vol 5-3.iso / More Goodies / More For Your Game / Realmz / Character Master Source / Nemesis Framework / Sources / nemesis dialogs.cpp < prev    next >
Text File  |  1996-07-03  |  25KB  |  1,037 lines

  1. //••••••••••••••••••••••••••••••••••••
  2. //    Some dialog utilities
  3. //••••••••••••••••••••••••••••••••••••
  4.  
  5. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  6. // Some required externals
  7. extern    void    HandleEventHook( EventRecord * );
  8. extern    void    AlertUpdateMenusHook();
  9. extern    nemesisGlobalPtr    G;
  10. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  11. #pragma mark ••••• Dialog Items •••••
  12. Handle    NemesisGetDialogItemHandle( DialogRef theDialog, int itemNumber )
  13. {
  14.     Rect    tempRect;
  15.     short    tempItemType = -1;
  16.     Handle    tempHandle;
  17.     
  18.     GetDialogItem( theDialog, itemNumber, &tempItemType, &tempHandle, &tempRect );
  19.     
  20.     if( tempItemType < 0 )    // assume the call failed
  21.         tempHandle = nil;
  22.     
  23.     return tempHandle;
  24. }
  25.  
  26. short    NemesisGetDialogItemType( DialogRef theDialog, int itemNumber )
  27. {
  28.     Rect    tempRect;
  29.     short    tempItemType = -1;
  30.     Handle    tempHandle;
  31.     
  32.     GetDialogItem( theDialog, itemNumber, &tempItemType, &tempHandle, &tempRect );
  33.     
  34.     return tempItemType;
  35. }
  36.  
  37. void    NemesisGetDialogItemRect( DialogRef theDialog, int theItem, Rect &theRect )
  38. {
  39.     short        itemType = -1;
  40.     Handle        itemHandle;
  41.  
  42.     GetDialogItem(theDialog, theItem, &itemType, &itemHandle, &theRect);
  43.     if(itemType < 0)
  44.         SetRect( &theRect, 0, 0, 0, 0 );
  45. }
  46.  
  47. Boolean NemesisFlashItem (DialogRef theDialog, EventRecord *theEvent, short itemHit)
  48. {
  49.     Boolean result = false; // assume it wasn't a button
  50.     Rect    itemRect;
  51.     Handle    itemHandle;
  52.     Point    mousePt;
  53.     long    temp;
  54.  
  55.     if ( NemesisGetDialogItemType( theDialog, itemHit ) != kButtonDialogItem)
  56.     {
  57.         return result;    // not a button
  58.     }
  59.     // if item 1 is a button, assume it is the OK button and assume item 2 is cancel
  60.     
  61.     itemHandle = NemesisGetDialogItemHandle(theDialog, itemHit);
  62.     if( itemHandle == nil ) return result;    // Check for nil handle
  63.     
  64.     // set event->where to button coordinates
  65.     NemesisGetDialogItemRect( theDialog, itemHit, itemRect );
  66.     mousePt.h = itemRect.left + 1;
  67.     mousePt.v = itemRect.top + 1;
  68.     NemesisLocalToGlobal (theDialog, mousePt, &mousePt);
  69.     theEvent->where = mousePt;    // event->where always global
  70.  
  71.     // flash the button
  72.     HiliteControl( (ControlHandle)itemHandle, 1 );
  73.     Delay( 8, &temp );
  74.     HiliteControl( (ControlHandle)itemHandle, 0 );
  75.  
  76.     // make it a mousedown event
  77.     theEvent->what = 1;
  78.  
  79.     result = true;
  80.     return result;
  81. }
  82.  
  83. #pragma mark ••••• Text Edit Items •••••
  84.  
  85. void    NemesisNumToItemText( DialogRef theDialog, int theItem, long theNumber )
  86. {
  87.     Str255    tempStr;
  88.     
  89.     NumToString( theNumber, tempStr );
  90.     
  91.     NemesisSetItemText( theDialog, theItem, tempStr );
  92. }
  93.  
  94. void    NemesisSetItemText( DialogRef theDialog, int theItem, Str255 theString )
  95. {
  96.     Handle    theItemHandle;
  97.     Str255    tempStr;
  98.     
  99.     if( NemesisGetDialogItemType( theDialog, theItem ) == kEditTextDialogItem )
  100.     {
  101.         NemesisGetItemText( theDialog, theItem, tempStr );
  102.         if( !EqualString( tempStr, theString, true, true ) )
  103.         {
  104.             theItemHandle = NemesisGetDialogItemHandle( theDialog, theItem );
  105.             SetDialogItemText( theItemHandle, theString );
  106.         }
  107.     }
  108. }
  109.  
  110. long    NemesisItemTextToNum( DialogRef theDialog, int theItem )
  111. {
  112.     Str255    tempStr;
  113.     long    theNumber;
  114.     int        count;
  115.     int        startCount = 0;
  116.     Boolean    valid = true;
  117.     
  118.     NemesisGetItemText( theDialog, theItem, tempStr );
  119.     if( tempStr[0] == 0 )    return 0;
  120.     
  121.     if( tempStr[0] > 9 )
  122.     {
  123.         if( tempStr[1] == 45 )    // First char is -
  124.         {
  125.             if( tempStr[0] > 11 )    // First check: number to long
  126.             {
  127.                 valid = false;
  128.             }
  129.             else if( ((tempStr[0] >10) && (tempStr[2] > '2')) ||
  130.                      ((tempStr[0] >10) && (tempStr[3] > '0')) ||
  131.                      ((tempStr[0] >10) && (tempStr[4] > '0')) ||
  132.                      ((tempStr[0] >10) && (tempStr[5] > '0')) ||
  133.                      ((tempStr[0] >10) && (tempStr[6] > '0')) ||
  134.                      ((tempStr[0] >10) && (tempStr[7] > '0')) ||
  135.                      ((tempStr[0] >10) && (tempStr[8] > '0')) ||
  136.                      ((tempStr[0] >10) && (tempStr[9] > '0')) ||
  137.                      ((tempStr[0] >10) && (tempStr[10] > '0')) ||
  138.                      ((tempStr[0] >10) && (tempStr[11] > '0')) )
  139.             {
  140.                 valid = false;
  141.             }
  142.         }
  143.         else
  144.         {
  145.             if( tempStr[0] > 10 )    // First check: number to long
  146.             {
  147.                 valid = false;
  148.             }
  149.             else if( ((tempStr[0] >10) && (tempStr[1] > '2')) ||
  150.                      ((tempStr[0] >10) && (tempStr[2] > '0')) ||
  151.                      ((tempStr[0] >10) && (tempStr[3] > '0')) ||
  152.                      ((tempStr[0] >10) && (tempStr[4] > '0')) ||
  153.                      ((tempStr[0] >10) && (tempStr[5] > '0')) ||
  154.                      ((tempStr[0] >10) && (tempStr[6] > '0')) ||
  155.                      ((tempStr[0] >10) && (tempStr[7] > '0')) ||
  156.                      ((tempStr[0] >10) && (tempStr[8] > '0')) ||
  157.                      ((tempStr[0] >10) && (tempStr[9] > '0')) ||
  158.                      ((tempStr[0] >10) && (tempStr[10] > '0')) )
  159.             {
  160.                 valid = false;
  161.             }
  162.         }
  163.     }
  164.     
  165.     // Now to check for letters in the number
  166.     if( valid )
  167.     {
  168.         if( tempStr[1] == 45 )    // First char is -
  169.             startCount = 2;
  170.         else
  171.             startCount = 1;
  172.  
  173.         for( count = startCount; count <= tempStr[0]; count++ )
  174.         {
  175.             if( (tempStr[count] < 48) || (tempStr[count] > 57) )
  176.                 valid = false;
  177.         }
  178.     }
  179.             
  180.     if( valid )
  181.         StringToNum( tempStr, &theNumber );
  182.     else
  183.         theNumber = -2000000001; // One smaller than the min I'm setting
  184.     
  185.     return theNumber;
  186. }
  187.  
  188. void    NemesisGetItemText( DialogRef theDialog, int theItem, Str255 theString )
  189. {
  190.     Handle    theItemHandle;
  191.     
  192.     theString[0] = 0;    // Assume failure
  193.  
  194.     if( NemesisGetDialogItemType( theDialog, theItem ) == kEditTextDialogItem )
  195.     {
  196.         theItemHandle = NemesisGetDialogItemHandle( theDialog, theItem );
  197.         GetDialogItemText( theItemHandle, theString );
  198.     }
  199. }
  200.  
  201. void    NemesisSelectItemText( DialogRef theDialog, int theItemNumber )
  202. {
  203.     if( NemesisGetDialogItemType( theDialog, theItemNumber ) == kEditTextDialogItem )
  204.         SelectDialogItemText( theDialog, theItemNumber, 0, 32767 );
  205. }
  206.  
  207. #pragma mark ••••• Dialog Fonts •••••
  208.  
  209. void    NemesisSetFont( WindowRef theWindow, Str255 fName, int fSize, int fStyle )
  210. {
  211.     short    fNum;
  212.     GrafPtr    oldPort;
  213.     
  214.     // Save the current port
  215.     GetPort( &oldPort );
  216.     SetPort( theWindow );
  217.     
  218.     GetFNum( fName, &fNum );
  219.     TextFont( fNum );
  220.     TextFace( fStyle );
  221.     TextSize( fSize );
  222.     TextMode( patCopy );
  223.     
  224.     // Restore original port
  225.     SetPort( oldPort );
  226. }
  227.  
  228. void    NemesisSetFont( WindowRef theWindow, int fNum, int fSize, int fStyle )
  229. {
  230.     GrafPtr    oldPort;
  231.     
  232.     // Save the current port
  233.     GetPort( &oldPort );
  234.     SetPort( theWindow );
  235.     
  236.     TextFont( fNum );
  237.     TextFace( fStyle );
  238.     TextSize( fSize );
  239.     TextMode( patCopy );
  240.     
  241.     // Restore original port
  242.     SetPort( oldPort );
  243. }
  244.  
  245. #pragma mark ••••• Dialog Strings •••••
  246.  
  247. void    NemesisDrawString( WindowRef theWindow, int x, int y, Str255 theString )
  248. {
  249.     Point    oldPosition;
  250.     GrafPtr    oldPort;
  251.     
  252.     // Save the current port
  253.     GetPort( &oldPort );
  254.     SetPort( theWindow );
  255.  
  256.     GetPen( &oldPosition );
  257.     
  258.     MoveTo( x, y );
  259.     
  260.     DrawString( theString );
  261.     
  262.     MoveTo( oldPosition.h, oldPosition.v );
  263.     // Restore original port
  264.     SetPort( oldPort );
  265. }
  266.  
  267. #pragma mark ••••• Dialog Controls ••••••••
  268. Boolean    NemesisIsItemAControl( DialogRef theDialog, int theItem )
  269. {
  270.     int itemType = NemesisGetDialogItemType( theDialog, theItem );
  271.     Boolean    isControl = false;
  272.     
  273.     if( itemType == kControlDialogItem )
  274.         isControl = true;
  275.     else
  276.     {
  277.         switch( itemType )
  278.         {
  279.             case kButtonDialogItem:
  280.             case kCheckBoxDialogItem:
  281.             case kRadioButtonDialogItem:
  282.             case kResourceControlDialogItem: isControl = true; break;
  283.             default : isControl = false; break;
  284.         }
  285.     }
  286.     
  287.     return isControl;
  288. }
  289.  
  290. void    NemesisSetControlTitle( DialogRef theDialog, int theItem, const Str255 theTitle )
  291. {
  292.     Handle    theItemHandle;
  293.     Str255    tempStr;
  294.     
  295.     if( NemesisIsItemAControl( theDialog, theItem ) )
  296.     {
  297.         NemesisGetControlTitle( theDialog, theItem, tempStr );
  298.         
  299.         theItemHandle = NemesisGetDialogItemHandle( theDialog, theItem );
  300.         if( theItemHandle )
  301.         {
  302.             if( !EqualString( tempStr, theTitle, true, true ) )
  303.                 SetControlTitle( (ControlRef)theItemHandle, theTitle );
  304.         }
  305.     }
  306. }
  307.  
  308. void    NemesisGetControlTitle( DialogRef theDialog, int theItem, Str255 theTitle )
  309. {
  310.     Handle    theItemHandle;
  311.     theTitle[0] = 0;    // Nullify it in case of failure
  312.  
  313.     if( NemesisIsItemAControl( theDialog, theItem ) )
  314.     {    
  315.         theItemHandle = NemesisGetDialogItemHandle( theDialog, theItem );
  316.         if( theItemHandle )
  317.             GetControlTitle( (ControlRef)theItemHandle, theTitle );
  318.     }
  319. }
  320.  
  321. int        NemesisGetControlValue( DialogRef theDialog, int theItem )
  322. {
  323.     Handle    theItemHandle;
  324.     int        temp = 0;
  325.     
  326.     if( NemesisIsItemAControl( theDialog, theItem ) )
  327.     {    
  328.         theItemHandle = NemesisGetDialogItemHandle( theDialog, theItem );
  329.         if( theItemHandle )
  330.             temp = GetControlValue( (ControlRef)theItemHandle );
  331.     }
  332.     
  333.     return temp;
  334. }
  335.  
  336. void    NemesisSetControlValue( DialogRef theDialog, int theItem, int theValue )
  337. {
  338.     Handle    theItemHandle;
  339.     
  340.     if( NemesisIsItemAControl( theDialog, theItem ) )
  341.     {    
  342.         theItemHandle = NemesisGetDialogItemHandle( theDialog, theItem );
  343.         if( theItemHandle )
  344.             SetControlValue( (ControlRef)theItemHandle, theValue );
  345.     }
  346. }
  347.  
  348. void    NemesisDimControl( DialogRef theDialog, int theItem )
  349. {
  350.     Handle    theControl;
  351.     
  352.     if( NemesisIsItemAControl( theDialog, theItem ) )
  353.     {
  354.         theControl = NemesisGetDialogItemHandle( theDialog, theItem );
  355.         if( theControl )
  356.             HiliteControl( (ControlRef)theControl, 255 );
  357.     }
  358. }
  359.  
  360. void    NemesisUndimControl( DialogRef theDialog, int theItem )
  361. {
  362.     Handle    theControl;
  363.     
  364.     if( NemesisIsItemAControl( theDialog, theItem ) )
  365.     {    
  366.         theControl = NemesisGetDialogItemHandle( theDialog, theItem );
  367.         if( theControl )
  368.             HiliteControl( (ControlRef)theControl, 0 );
  369.     }
  370. }
  371.  
  372. #pragma mark ••••• Dialog filters and user items •••••
  373.  
  374. pascal    Boolean    NemesisModalFilter( DialogRef theDialog, EventRecord * theEvent, short *itemHit)
  375. {
  376.     Boolean        callNormalHandler = false;    /* call our event handler? */
  377.     Boolean        result              = false;    /* to return to ModalDialog */
  378.     char        theKey;
  379.     WindowRef    hitWindow;
  380.     short        windowPart;
  381.     
  382.     windowPart = FindWindow(theEvent->where, &hitWindow);
  383.     
  384.     switch(theEvent->what)
  385.     {
  386.         // We update/activate app windows, ModalDialog() does dialog window.
  387.         case updateEvt:
  388.         case activateEvt:
  389.             if ( IsNemesisWindow( (WindowRef)theEvent->message) )    // an app window?
  390.             {
  391.                 callNormalHandler = true;
  392.             }
  393.             break;
  394.         /*    Allow command dragging a window other than the dialog. This is
  395.             really cool, because it allows you to move a hidden window
  396.             out from behind a modal dialog so you can see what's going on.
  397.             
  398.             All other mouse clicks go to ModalDialog(). */
  399.         case mouseDown:
  400.             if ( theEvent->modifiers & cmdKey )
  401.             {
  402.  
  403.                 if (windowPart == inDrag  && (hitWindow != theDialog))
  404.                 {
  405.                     callNormalHandler = true;
  406.                 }
  407.             }
  408.             break;
  409.         /*    All keystrokes go to ModalDialog(). Except we want to detect
  410.             command period keystroke for cancel, and return/enter key to
  411.             hit the default button. */
  412.         case keyDown:
  413.         case autoKey:
  414.             theKey = (char)(theEvent->message & charCodeMask); // get the key pressed
  415.             if (theEvent->modifiers & cmdKey)    // command key down
  416.             {
  417.                 if(theKey == '.')    // if it's the period
  418.                 {
  419.                     result = NemesisFlashItem(theDialog, theEvent, cancel);
  420.                     *itemHit = cancel;
  421.                 }
  422.             }
  423.             else    // no command key
  424.             {
  425.                 if (theKey == 0x0D || theKey == 0x03)    // return or enter
  426.                 {
  427.                     result = NemesisFlashItem(theDialog, theEvent, ok);
  428.                     *itemHit = ok;
  429.                 }
  430.                 else if (theKey == 0x1B)    // escape key
  431.                 {
  432.                     result = NemesisFlashItem(theDialog, theEvent, cancel);
  433.                     *itemHit = cancel;
  434.                 }
  435.             }
  436.             break;
  437.         //    Handle whatever other events are coming in such as
  438.         //    null events, AppleEvents, etc. 
  439.         default:
  440.             callNormalHandler = true;
  441.             break;
  442.             
  443.     }
  444.     
  445.     // if we're going to take care of it, call the normal handler
  446.     if (callNormalHandler)
  447.     {
  448.         HandleEventHook( theEvent );
  449.     }
  450.     
  451.     return result;
  452. }
  453.  
  454. void    NemesisSetupDefaultButton( DialogRef theDialog, short defaultItem)
  455. {
  456.     short        itemType;
  457.     Rect        itemRect;
  458.     Handle        itemHandle;    
  459.     
  460.     GetDialogItem(theDialog, defaultItem, &itemType, &itemHandle, &itemRect);
  461.     
  462.     // a little error control here 
  463.     itemType &= ~kItemDisableBit; // don't care it it's enabled or not
  464.                         
  465.     if (itemType != kUserDialogItem)
  466.         return;
  467.     else
  468.         SetDialogItem(    theDialog,
  469.                         defaultItem,
  470.                         itemType,
  471.                         (Handle) G->DrawDefaultButtonUPP(),
  472.                         &itemRect );
  473. }
  474.  
  475. pascal    void    NemesisDrawDefaultButton( DialogRef theDialog, short item )
  476. {
  477.     OSErr        error = noErr;
  478.     short        itemType = NemesisGetDialogItemType( theDialog, item );
  479.     Rect        itemRect;
  480.     NemesisGetDialogItemRect( theDialog, item, itemRect );
  481.     PenState    oldPen;
  482.     RGBColor    oldForeColour;
  483.     Boolean        inBack = false;
  484.  
  485.     // just to be safe, let's make sure it's a button
  486.     if ( itemType != kButtonDialogItem )
  487.         return;
  488.  
  489.     // save current pen
  490.     GetPenState(&oldPen);
  491.  
  492.     // set it to what we want
  493.     PenNormal();
  494.     PenSize(3,3);
  495.     
  496.     // dialog could be modeless. If in the background, set pen to grey
  497.     if ( theDialog != FrontWindow() || !G->SwitchedIn() )
  498.     {
  499.         inBack = true;
  500.         NemesisSetDimmedColour( itemRect, oldForeColour );
  501.     }
  502.  
  503.     // outline it
  504.     InsetRect( &itemRect, -4, -4 );
  505.     FrameRoundRect( &itemRect, 16, 16 );
  506.  
  507.     // restore original pen
  508.     if ( inBack )
  509.         NemesisRestoreDimmedColour( oldForeColour );
  510.     SetPenState( &oldPen );
  511. }
  512.  
  513. void    NemesisSetupOutline( DialogRef theDialog, short outlineItem)
  514. {
  515.     short        itemType = NemesisGetDialogItemType( theDialog, outlineItem );
  516.     Rect        itemRect;
  517.     NemesisGetDialogItemRect( theDialog, outlineItem, itemRect );
  518.     
  519.     // a little error control here 
  520.     itemType &= ~kItemDisableBit; // don't care it it's enabled or not
  521.                         
  522.     if (itemType != kUserDialogItem)
  523.         return;
  524.     else
  525.         SetDialogItem(    theDialog,
  526.                         outlineItem,
  527.                         itemType,
  528.                         (Handle) G->DrawOutlineUPP(),
  529.                         &itemRect);
  530. }
  531.  
  532. pascal    void    NemesisDrawOutline( DialogRef theDialog, short item )
  533. {
  534.     OSErr        error = noErr;
  535.     Rect        itemRect;
  536.     PenState    oldPen;
  537.     RGBColor    oldForeColour;
  538.     Boolean        didGrey;
  539.         
  540.     // Get the items Rect
  541.     NemesisGetDialogItemRect( theDialog, item, itemRect);
  542.  
  543.     // Save the current state
  544.     GetPenState( &oldPen );
  545.  
  546.     // set it to what we want
  547.     PenNormal();
  548.     didGrey = NemesisSetDimmedColour( itemRect, oldForeColour );
  549.     if( !didGrey )
  550.         PenPat( &qd.black );
  551.  
  552.     // Make the outline a bit bigger than the item
  553.     InsetRect( &itemRect, -3, -3 );
  554.     FrameRect( &itemRect );
  555.  
  556.     // restore original pen
  557.     if( didGrey )
  558.         NemesisRestoreDimmedColour( oldForeColour );
  559.     SetPenState( &oldPen );
  560. }
  561.  
  562. #pragma mark ••••• Checkboxes and Radio buttons •••••
  563.  
  564. Boolean    NemesisCheckItemsAreRadios( DialogRef theDialog, int firstButton, int lastButton )
  565. {
  566.     int    count;
  567.     Boolean    theyAreRadios = true;
  568.     
  569.     for( count = firstButton; count <= lastButton; count++ )
  570.     {
  571.         if( NemesisGetDialogItemType( theDialog, count ) != kRadioButtonDialogItem )
  572.         {
  573.             theyAreRadios = false;
  574.             break;
  575.         }
  576.     }
  577.     
  578.     return theyAreRadios;
  579. }
  580.  
  581. void    NemesisSetActiveRadioButton( DialogRef theDialog, int firstButton, int lastButton, int theButton )
  582. {
  583.     int        count;
  584.     
  585.     if( NemesisCheckItemsAreRadios( theDialog, firstButton, lastButton ) )
  586.     {
  587.         // First make sure all the buttons are off
  588.         for( count = firstButton; count <= lastButton; count++ )
  589.         {
  590.             if( NemesisGetControlValue( theDialog, count ) != kControlRadioButtonUncheckedValue )
  591.                 NemesisSetControlValue( theDialog, count, kControlRadioButtonUncheckedValue );
  592.         }
  593.         // Now turn on the button we want
  594.         NemesisSetControlValue( theDialog, theButton, kControlRadioButtonCheckedValue );
  595.     }
  596. }
  597.  
  598. int        NemesisGetActiveRadioButton( DialogRef theDialog, int firstButton, int lastButton )
  599. {
  600.     int        count;
  601.     int        tempValue;
  602.     
  603.     count = (firstButton-1);
  604.  
  605.     if( NemesisCheckItemsAreRadios( theDialog, firstButton, lastButton ) )
  606.     {
  607.         do
  608.         {
  609.             ++count;
  610.             tempValue = NemesisGetControlValue( theDialog, count );
  611.         } while( (tempValue != kControlRadioButtonCheckedValue) && ( count <= lastButton ) );
  612.     }
  613.     else
  614.         count = -1;
  615.     
  616.     return count;
  617. }
  618.  
  619. void    NemesisToggleCheckbox( DialogRef theDialog, int theBox )
  620. {
  621.     if( NemesisGetDialogItemType( theDialog, theBox ) == kCheckBoxDialogItem )
  622.     {
  623.         if( NemesisGetControlValue( theDialog, theBox ) == kControlCheckboxCheckedValue )
  624.             NemesisUncheckCheckbox( theDialog, theBox );
  625.         else
  626.             NemesisCheckCheckbox( theDialog, theBox );
  627.     }
  628. }
  629.  
  630. void    NemesisSetCheckbox( DialogRef theDialog, int theBox, int theValue )
  631. {
  632.     if( NemesisGetDialogItemType( theDialog, theBox ) == kCheckBoxDialogItem )
  633.     {
  634.         if( NemesisGetCheckboxValue( theDialog, theBox ) != theValue )
  635.             NemesisSetControlValue( theDialog, theBox, theValue );
  636.     }
  637. }
  638.  
  639. void    NemesisCheckCheckbox( DialogRef theDialog, int theBox )
  640. {
  641.     if( NemesisGetDialogItemType( theDialog, theBox ) == kCheckBoxDialogItem )
  642.     {
  643.         if( NemesisGetCheckboxValue( theDialog, theBox ) != kControlCheckboxCheckedValue )
  644.             NemesisSetControlValue( theDialog, theBox, kControlCheckboxCheckedValue );
  645.     }
  646. }
  647.  
  648. void    NemesisUncheckCheckbox( DialogRef theDialog, int theBox )
  649. {
  650.     if( NemesisGetDialogItemType( theDialog, theBox ) == kCheckBoxDialogItem )
  651.     {
  652.         if( NemesisGetCheckboxValue( theDialog, theBox ) != kControlCheckboxUncheckedValue )
  653.             NemesisSetControlValue( theDialog, theBox, kControlCheckboxUncheckedValue );
  654.     }
  655. }
  656.  
  657. int        NemesisGetCheckboxValue( DialogRef theDialog, int theBox )
  658. {
  659.     int    temp    = -1;
  660.     
  661.     if( NemesisGetDialogItemType( theDialog, theBox ) == kCheckBoxDialogItem )
  662.     {
  663.         temp = NemesisGetControlValue( theDialog, theBox );
  664.     }
  665.  
  666.     return temp;
  667. }
  668.  
  669. #pragma mark ••••• Nemesis Alert Dialogs ••••
  670.  
  671. void    alertDialog::DoUpdateMenus()
  672. {
  673.     AlertUpdateMenusHook();
  674. }
  675.  
  676. OSErr    NemesisGetAlert( int dialogID, Str255 theText )
  677. {
  678.     OSErr        error = noErr;
  679.     DialogRef    theDialog;
  680.             
  681.     error = NemesisGetDialog( dialogID, nil, kInFront, theDialog );
  682.     if( error ) return error;
  683.  
  684.     NemesisSetupDefaultButton( theDialog, iAlertDefault );
  685.  
  686.     // Stuff a pointer to the dialog in the WinRefCon
  687.     SetWRefCon( (WindowRef)theDialog,
  688.                 (long)(new alertDialog(theDialog, nil)) );
  689.     
  690.     NemesisSetDialogKind( theDialog, kAlertDKind );
  691.  
  692.     // set the text in the dialog
  693.     ParamText( theText, "\p", "\p", "\p" );
  694.  
  695.     NemesisShowDialog( theDialog );
  696.  
  697.     // beep to alert the user
  698.     SysBeep( 30 );
  699.     return error;
  700. }
  701.  
  702. OSErr    NemesisStopAlert( Str255 theString )
  703. {
  704.     OSErr    error = noErr;
  705.     
  706.     error = NemesisGetAlert( kStopID, theString );
  707.  
  708.     return error;
  709. }
  710.  
  711. OSErr    NemesisCautionAlert( Str255 theString )
  712. {
  713.     OSErr    error = noErr;
  714.     
  715.     error = NemesisGetAlert( kCautionID, theString );
  716.  
  717.     return error;
  718. }
  719.  
  720. OSErr    NemesisNoteAlert( Str255 theString )
  721. {
  722.     OSErr    error = noErr;
  723.     
  724.     error = NemesisGetAlert( kNoteID, theString );
  725.  
  726.     return error;
  727. }
  728.  
  729. OSErr    NemesisStopAlert( int theIndID, int theStringNum )
  730. {
  731.     OSErr    error = noErr;
  732.     Str255    theString;
  733.     
  734.     // get the message string
  735.     NemesisGetIndString ( theIndID, theStringNum, theString );
  736.  
  737.     error = NemesisStopAlert( theString );
  738.  
  739.     return error;
  740. }
  741.  
  742. OSErr    NemesisCautionAlert( int theIndID, int theStringNum )
  743. {
  744.     OSErr    error = noErr;
  745.     Str255    theString;
  746.     
  747.     // get the message string
  748.     NemesisGetIndString ( theIndID, theStringNum, theString );
  749.  
  750.     error = NemesisCautionAlert( theString );
  751.  
  752.     return error;
  753. }
  754.  
  755. OSErr    NemesisNoteAlert( int theIndID, int theStringNum )
  756. {
  757.     OSErr    error = noErr;
  758.     Str255    theString;
  759.     
  760.     // get the message string
  761.     NemesisGetIndString ( theIndID, theStringNum, theString );
  762.  
  763.     error = NemesisNoteAlert( theString );
  764.  
  765.     return error;
  766. }
  767.  
  768. #pragma mark ••••• About Dialog •••••
  769.  
  770. void    aboutDialogClass::DoFilterEvent( EventRecord *theEvent )
  771. {
  772.     // Update the animation
  773.     if( !simpleAbout ) theCredits->DoAnimation();
  774.  
  775.     // Call the base class for the rest of the stuff
  776.     baseDialog::DoFilterEvent( theEvent );
  777. };
  778.  
  779. void    aboutDialogClass::DoUpdateMenus()
  780. {
  781.     AlertUpdateMenusHook();
  782. };
  783.  
  784. void    aboutDialogClass::DoItemHit( EventRecord *theEvent, int itemHit )
  785. {
  786.     switch( itemHit )
  787.     {
  788.         case ok : NemesisPlayGoodClick();    // Play click sound
  789.                   if( !simpleAbout )
  790.                       delete theCredits;
  791.                   else
  792.                       DisposeDialog( thisDialog );
  793.                       
  794.                   NemesisUpdateMenus( FrontWindow() );
  795.                   break;
  796.         default : NemesisPlayBadClick();
  797.                   break;
  798.     }
  799. };
  800.  
  801. OSErr    NemesisAboutBox( int theDialogID, int theDefaultButton, int theScrollItem )
  802. {
  803.     CGrafPtr    theDialog;
  804.     OSErr        error = noErr;
  805.  
  806.     error = NemesisGetDialog( theDialogID, nil, kInFront, ((DialogRef)(theDialog)) );
  807.     if( error ) return error;
  808.  
  809.     NemesisSetupDefaultButton( (DialogRef)theDialog, theDefaultButton );
  810.  
  811.     // Stuff a pointer to the dialog in the WinRefCon
  812.     SetWRefCon( (WindowRef)theDialog,
  813.                 (long)(new aboutDialogClass((DialogRef)theDialog, nil)) );
  814.     
  815.     NemesisSetDialogKind( ((DialogRef)theDialog), kAboutScrollDKind );
  816.  
  817.     if( !NemesisControlKeyDown() )
  818.     {
  819.         // Setup the credits
  820.         ((aboutDialogClass *)(NemesisGetBaseDPtr( (DialogRef)theDialog )))->SetCredits( new creditsScroll(theScrollItem, theDialog) );
  821.  
  822.         // Check to see if there was an error during construction
  823.         error = ( (aboutDialogClass *)(NemesisGetBaseDPtr( (DialogRef)theDialog )) )->InitError();
  824.         if( error )
  825.         {
  826.             // So we only notify the user once.
  827.             if( G->HadFancyAboutMemError() )
  828.             {
  829.                 switch(error)
  830.                 {
  831.                     case memFullErr:
  832.                     case cTempMemErr:
  833.                     case cNoMemErr: error = noErr;
  834.                                     break;
  835.                 }
  836.             }
  837.             else
  838.             {
  839.                 G->SetHadFancyAboutMemError();
  840.                 switch(error)
  841.                 {
  842.                     case memFullErr:
  843.                     case cTempMemErr:
  844.                     case cNoMemErr: error = kNotEnoughMemForFancyAbout;
  845.                                     break;
  846.                 }
  847.             }
  848.             ((aboutDialogClass *)(NemesisGetBaseDPtr( (DialogRef)theDialog )))->MakeItSimple();
  849.         }
  850.         else
  851.         {
  852.             ShortenDITL( (DialogRef)theDialog, 1 );    // Get rid of last picture
  853.         }
  854.     }
  855.     else
  856.     {
  857.         ((aboutDialogClass *)(NemesisGetBaseDPtr( (DialogRef)theDialog )))->MakeItSimple();
  858.     }
  859.     
  860.     NemesisShowDialog( ((DialogRef)theDialog) );
  861.  
  862.     return error;
  863. }
  864.  
  865. #pragma mark ••••• Linked list maintanence •••••
  866.  
  867. void    NemesisAddDialogToList( DialogRef theDialog )
  868. {
  869.     if( !theDialog )
  870.         return;    // Check we were not slipped a nil
  871.         
  872.     DialogRef        thisDialog;
  873.     baseModelessDialogPtr    oldLastDialog;
  874.     
  875.     if ( !G->DialogList() ) // This is the first dialog
  876.     {
  877.         G->SetDialogList( theDialog ); // Set the dialog list to point to the first dialog
  878.         
  879.         // These next variable _should_ have been initialised correctly
  880.         // but I'm setting them here again, just in case
  881.         (NemesisGetBaseMlessPtr( theDialog ))->SetNextDialog( nil );
  882.         (NemesisGetBaseMlessPtr( theDialog ))->SetPrevDialog( nil );
  883.     }
  884.     else // Find old last dialog and introduce it to the new dialog
  885.     {
  886.         // Start at the beginning of the list and walk through it till
  887.         // we get to the old last dialog.
  888.         thisDialog = G->DialogList();
  889.     
  890.         while( thisDialog )
  891.         {
  892.             oldLastDialog = NemesisGetBaseMlessPtr( thisDialog );
  893.             thisDialog = oldLastDialog->NextDialog();
  894.         }
  895.         
  896.         // Plop the new dialog after the old last dialog
  897.         oldLastDialog->SetNextDialog( theDialog );
  898.  
  899.         // Tell the new dialog about its ancestors( the old last dialog )
  900.         (NemesisGetBaseMlessPtr( theDialog ))->SetPrevDialog( oldLastDialog->ThisDialog() );
  901.     }
  902. }
  903.  
  904. void    NemesisRemoveDialogFromList ( DialogRef theDialog )
  905. {        
  906.     DialogRef        previousDialog;
  907.     DialogRef        nextDialog;
  908.     baseModelessDialogPtr    theMlessDialog = NemesisGetBaseMlessPtr( theDialog );
  909.  
  910.     // get previous and next dialogs, either could be nil values
  911.     previousDialog    = theMlessDialog->PrevDialog();
  912.     nextDialog        = theMlessDialog->NextDialog();
  913.     
  914.     if( previousDialog ) // There is a dialog before this one
  915.     {
  916.         // Tell it about its promotion and who the new boss is
  917.         // (well ok, point it to the new next dialog)
  918.         (NemesisGetBaseMlessPtr( previousDialog ))->SetNextDialog( nextDialog );
  919.  
  920.         if ( nextDialog )
  921.         {
  922.             // Tell the next dialog its underling has been sacked and
  923.             // show it the new junior.
  924.             // (well, set the next dialog to the previous dialog, cutting out the 
  925.             //  middle man so to speak…)
  926.             (NemesisGetBaseMlessPtr( nextDialog ))->SetPrevDialog( previousDialog );
  927.         }
  928.     }
  929.     else  // There is no dialog before the one we are closing
  930.     {
  931.         // Put the next dialog at the bottom of the pile
  932.         G->SetDialogList( nextDialog );
  933.  
  934.         if ( nextDialog ) // Current next dialog is now the first dialog
  935.         {
  936.             // Get rid of the prevDialog since there isn't one
  937.             (NemesisGetBaseMlessPtr( nextDialog ))->SetPrevDialog( (DialogRef)nil );
  938.         }
  939.     }
  940. }
  941.  
  942. OSErr    NemesisCloseAllDialogs()
  943. {
  944.     OSErr            error      = noErr;
  945.     DialogRef        theDialog  = (DialogRef)FrontWindow();
  946.     baseModelessDialogPtr    myDialog;
  947.     
  948.     while ( theDialog )
  949.     {
  950.         myDialog =  NemesisGetBaseMlessPtr( theDialog );
  951.          // Make sure we got a baseModelessDialogPtr from it
  952.         if( myDialog )
  953.         {
  954.             // close this dialog, if cancelled, return error
  955.             error = myDialog->DoClose();
  956.             if (error)
  957.             {
  958.                 return error;
  959.             }
  960.         }
  961.         // Get the next dialog
  962.         theDialog = (DialogRef)GetNextWindow( (WindowRef)theDialog );
  963.     }
  964.  
  965.     return error;
  966. }
  967.  
  968. OSErr    NemesisCloseThisDialog( DialogRef theDialog )
  969. {
  970.     OSErr            error      = noErr;
  971.     baseModelessDialogPtr    myDialog;
  972.     
  973.     myDialog =  NemesisGetBaseMlessPtr( theDialog );
  974.     // Make sure we got a baseModelessDialogPtr from it
  975.     if( myDialog )
  976.     {
  977.         // close this dialog, if cancelled, return error
  978.         error = myDialog->DoClose();
  979.     }
  980.  
  981.     return error;
  982. }
  983.  
  984. #pragma mark ••••• Misc Dialog Utilities •••••
  985.  
  986. void    NemesisPlayGoodClick()
  987. {
  988.     SHPlayByID( sClick, nil );
  989. }
  990.  
  991. void    NemesisPlayBadClick()
  992. {
  993.     SHPlayByID( sBadClick, nil );
  994. }
  995.  
  996. void    NemesisSetDialogKind( DialogRef &theDialog, int theKind )
  997. {
  998.     (NemesisGetBaseDPtr( theDialog ))->SetDialogKind( theKind );
  999. }
  1000.  
  1001. void    NemesisShowDialog( DialogRef &theDialog )
  1002. {
  1003.     SelectWindow( theDialog );
  1004.     NemesisUpdateMenus( FrontWindow() );
  1005.     ShowWindow( theDialog );
  1006. }
  1007.  
  1008. short    NemesisIsDialog( WindowRef theWindow )
  1009. {
  1010.     Boolean result = kNotDialog;
  1011.     short    windowVariant;
  1012.  
  1013.     if ( theWindow )
  1014.     {
  1015.         if ( GetWindowKind( theWindow ) == dialogKind )    // This may change when Copland comes
  1016.         {
  1017.             windowVariant = GetWVariant( theWindow );
  1018.             switch ( windowVariant )
  1019.             {
  1020.                 case dBoxProc:
  1021.                 case plainDBox:
  1022.                 case altDBoxProc:
  1023.                     result = kModal;
  1024.                     break;
  1025.                 
  1026.                 case noGrowDocProc:
  1027.                     result = kModeless;
  1028.                     break;
  1029.                     
  1030.                 case movableDBoxProc:
  1031.                     result = kMovableModal;
  1032.                     break;
  1033.             }
  1034.         }
  1035.     }
  1036.     return(result);
  1037. }